import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/education/couser_center/education_quiz_history_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/education/quiz/history/model.dart';

class EducationQuizHistoryController extends GetxController {
  /// 教材ID
  final textbookId = Get.arguments as String?;

  final history = <QuizHistory>[];

  /// 获取历史记录
  Future<List<EducationQuizHistoryEntity>> getHistory(int num, int size) async {
    final resp = await HttpUtil().generateGetRequest<List<EducationQuizHistoryEntity>>(
      Interface.eduQuizHistory,
      queryParameters: {
        'bookId': textbookId,
        'pageNum': num,
        'pageSize': size,
      },
    ).execute();
    return resp.data ?? [];
  }

  /// 根据状态获取颜色
  static Color commonColorMapper(String p0) {
    switch (p0) {
      case '答错':
        return Colors.red;
      case '答对':
        return Colors.green;
      case '未答':
        return Colors.grey;
    }
    return Colors.blue;
  }

  /// 卡片右上角
  static Widget commonRightCorner(int? earn, int? total) {
    final result = _Result.fromInt(earn, total);
    return Container(
      decoration: BoxDecoration(
        color: result.color,
        borderRadius: BorderRadius.circular(4.w),
      ),
      height: 18.w,
      width: 54.w,
      child: Center(
        child: Text(
          result.text,
          style: TextStyle(fontSize: 10.w, color: Colors.white),
          textAlign: TextAlign.center,
        ),
      ),
    );
  }
}

enum _Result {
  /// 不合格
  notPass,

  /// 合格
  pass,

  /// 优秀
  good,
  ;

  static _Result fromInt(int? earn, int? total) {
    if (earn == null || total == null) {
      return notPass;
    }
    final percent = earn / total;
    if (percent >= 0.8) {
      return good;
    } else if (percent >= 0.6) {
      return pass;
    } else {
      return notPass;
    }
  }

  String get text {
    switch (this) {
      case notPass:
        return '不合格';
      case pass:
        return '合格';
      case good:
        return '优秀';
    }
  }

  Color get color {
    switch (this) {
      case notPass:
        return Colors.red;
      case pass:
        return Colors.blue;
      case good:
        return Colors.green;
    }
  }
}
